package com.dzm.xgb.core.controller.api;

import com.dzm.common.result.R;
import com.dzm.common.util.JwtUtils;
import com.dzm.xgb.core.pojo.vo.BorrowerVo;
import com.dzm.xgb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 作者：Dzm
 * 时间：2024-09-19-11:26
 * 功能：
 */

@RestController
@RequestMapping("/api/core/borrower")
@Api(tags = "借款人管理")
public class BorrowerController {

    @Autowired
    private BorrowerService borrowerService;

    @PostMapping("/save")
    public R save(@RequestBody BorrowerVo borrowerVo, @RequestHeader("token") String token) {
        Long userId = JwtUtils.getUserId(token);
        borrowerService.saveBorrowerByUserId(borrowerVo, userId);

        return R.ok().message("信息录入成功！");
    }

    @GetMapping("/auth/getBorrowerStatus")
    public R getBorrowerStatus(@RequestHeader("token") String token) {
        Long userId = JwtUtils.getUserId(token);

        //根据userid获取借款状态
        Integer status = borrowerService.getBorrowerStatus(userId);
        return R.ok().data("borrowerStatus", status);
    }

    @GetMapping("/getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        BigDecimal amount = borrowerService.getBorrowerAmount(userId);
        return R.ok().data("borrowAmount", amount);
    }

}
